<template>
  <div class="permission-table">
    <el-dialog
      :title="!permissionForm.id ? '新增' : '修改'"
      :visible.sync="permissionAddDialogVisible"
       width="50%" :center="true">
      <el-form :model="permissionForm" ref="permissionForm" label-width="80px">
          <el-form-item prop="name" label="名称" size="mini">
            <el-input v-model="permissionForm.name" placeholder="名称"></el-input>
          </el-form-item>
        <el-form-item prop="location" label="标识" size="mini">
          <el-input v-model="permissionForm.permission" placeholder="标识"></el-input>
        </el-form-item>
        <el-form-item prop="type" label="类型" size="mini">
          <el-radio-group v-model="permissionForm.type">
            <el-radio :label="0">菜单</el-radio>
            <el-radio :label="1">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="introduction" label="排序" size="mini">
          <el-input v-model="permissionForm.sort" placeholder="排序" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" type="warning" @click="permissionAddDialogVisible=false">取消</el-button>
        <el-button size="mini"  type="primary" @click="addPermission">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import singlePicUpload from '@/components/upload/singlePicUpload'
export default {
  name: 'permission-add-or-update',
  components: {
    singlePicUpload
  },
  data () {
    return {
      permissionAddDialogVisible: false,
      permissionForm: {
        id: '',
        name: '',
        type: '',
        permission: '',
        sort: ''
      }
    }
  },
  methods: {
    inputHandler (url) {
      console.log(url)
      console.log(1111)
      this.permissionForm.pic = url
    },
    init (id) {
      console.log(id)
      this.permissionForm.id = id || 0
      this.permissionAddDialogVisible = true
      this.$nextTick(() => {
        this.$refs['permissionForm'].resetFields()
        if (this.permissionForm.id) {
          this.$api.getPermissionById({
            params: {id: id}
          }).then((res) => {
            console.log(res.data)
            if (res.data && res.data.code === 200) {
              console.log(22222)
              this.permissionForm.name = res.data.data.name
              this.permissionForm.permission = res.data.data.permission
              this.permissionForm.type = res.data.data.type
              this.permissionForm.sort = res.data.data.sort
            }
          })
        }
      })
    },
    addPermission () {
      let that = this
      let params = {
        name: this.permissionForm.name,
        permission: this.permissionForm.permission,
        type: this.permissionForm.type,
        sort: this.permissionForm.sort
      }
      if (!this.permissionForm.id) {
        this.$api.addPermission(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.permissionAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        params.id = this.permissionForm.id
        this.$api.updatePermission(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.permissionAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
  .permission-table{
    margin: auto;
    width: 80%;
  }
  .permission-img{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    overflow: hidden;
  }
  body .el-table th.gutter{
    display: table-cell!important;
  }

</style>
